package com.ruoyi.project.fusion.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

public class ComputeUtils {
    public static double calculateStandardDeviation(List<Double> values) {
        double mean = values.stream().mapToDouble(val -> val).average().orElse(0.0);
        double variance = values.stream().mapToDouble(val -> Math.pow(val - mean, 2)).sum() / values.size();
        return Math.sqrt(variance);
    }

    public static float roundToTwoDecimalPlaces(float value) {
        BigDecimal bd = new BigDecimal(Float.toString(value));
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd.floatValue();
    }

    public static List<Double> normalizeValues(List<Double> values) {
        double min = values.stream().min(Double::compare).orElse(0.0);
        double max = values.stream().max(Double::compare).orElse(1.0);
        return values.stream()
                .map(value -> (max == min) ? 0.5 : (value - min) / (max - min))
                .collect(Collectors.toList());
    }
}
